package pv3d
{
	import ca.turbulent.media.Pyro;
	
	import flash.display.BitmapData;
	
	import org.papervision3d.materials.BitmapMaterial;
	
	import view.ToolBitmapData;
	import view.fl2mx.Fl2Mx;

	public class PyroVideoStreamMateria extends BitmapMaterial implements IPyro
	{
		private var player:Pyro;
		private var url:String;
		private var parent:Object;
		private var width:Number;
		private var height:Number;
		public function PyroVideoStreamMateria(asset:BitmapData=null, precise:Boolean=false)
		{
			super(asset, precise);
		}
		/**
		 * 初始化
		 */ 
		public function init(url:String,parent:Object,width:Number=200,height:Number=200):void
		{
			if(parent==null)
			{
				trace("容器不能为空!");
				return;
			}
			this.url=url;
			this.parent=parent;
			this.width=width;
			this.height=height;
			initPyroPlayer();
		}
		/**
		 * 初始化pyro播放器
		 */ 
		private function initPyroPlayer():void
		{
			player=new Pyro(this.width,this.height);
			this.parent.addChild(Fl2Mx.fl2Mx(player));
			player.play(this.url);
			player.autoPlay=false;
			player.visible=false;
		}
		/**
		 * 刷新图片
		 */ 
	    override public function updateBitmap():void
		{
			if(player.video!=null)
			{
				this.bitmap=ToolBitmapData.getInstance().drawDisplayObject(player.video);
			}
		}
		/**
		 * 获取pyro,可以对视频进行相关操作
		 */ 
		public function get pyro():Pyro
		{
			return player;
		}
		/**
		 * 销毁
		 */ 
		override public function destroy():void
		{
			super.destroy();
			if(player!=null)
			{
				if(player.parent!=null)
				{
					player.parent.removeChild(player);
				}
				player=null;
			}
		}
		//***************************************下面是实现IPyro接口***********************
		public function play():void
		{
			player.play();
		}
		public function pause():void
		{
			if(player.netStream!=null)
			{
				player.stop();
			}
		}
		public function seek(offset:Number):void
		{
			if(player.netStream!=null)
			{
				player.seek(offset);
			}
		} 
		public function get duration():Number
		{
			if(player.netStream!=null)
			{
				return player.duration;
			}
			return 1;
		}
		public function get time():Number
		{
			if(player.netStream!=null)
			{
				return player.time;
			}
			return 0;
		}
	}
}